home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSIMAGE1.C < prev    next >
C/C++ Source or Header  |  1993-05-17  |  16KB  |  543 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsimage1.c */
  20. /* Image procedures for Ghostscript library */
  21. /* This file is logically part of gsimage.c; we have split it out */
  22. /* to reduce the code working set. */
  23. #include "gx.h"
  24. #include "memory_.h"
  25. #include "gpcheck.h"
  26. #include "gserrors.h"
  27. #include "gxfixed.h"
  28. #include "gxarith.h"
  29. #include "gxmatrix.h"
  30. #include "gscspace.h"
  31. #include "gspaint.h"
  32. #include "gzstate.h"
  33. #include "gzdevice.h"            /* requires gsstate.h */
  34. #include "gzcolor.h"            /* requires gxdevice.h */
  35. #include "gzpath.h"
  36. #include "gxcolor.h"
  37. #include "gxcpath.h"
  38. #include "gxdevmem.h"
  39. #include "gximage.h"
  40.  
  41. /* Process the next piece of an image */
  42. int
  43. gs_image_next(register gs_image_enum *penum, byte *dbytes, uint dsize)
  44. {    uint rsize = penum->bytes_per_row;
  45.     uint pos = penum->byte_in_row;
  46.     int width = penum->width;
  47.     uint dleft = dsize;
  48.     uint dpos = 0;
  49.     gs_state *pgs = penum->pgs;
  50.     gx_device *save_dev = 0;
  51.     fixed xcur_save, ycur_save;
  52.     int y_save;
  53.     int code;
  54.     /* Accumulate separated colors, if needed */
  55.     if ( penum->plane_index == 0 )
  56.         penum->plane_size = dsize;
  57.     else if ( dsize != penum->plane_size )
  58.         return_error(gs_error_undefinedresult);
  59.     penum->planes[penum->plane_index] = dbytes;
  60.     if ( ++(penum->plane_index) != penum->spread )
  61.         return 0;
  62.     penum->plane_index = 0;
  63.     /* Save the dynamic state components in case of an error. */
  64.     xcur_save = penum->xcur;
  65.     ycur_save = penum->ycur;
  66.     y_save = penum->y;
  67.     /* We've accumulated an entire set of planes. */
  68.     if ( !penum->never_clip )
  69.        {    /* Install the clipping device if needed. */
  70.         gx_device *dev = (gx_device *)&penum->clip_dev;
  71.         save_dev = gs_currentdevice(pgs);
  72.         penum->clip_dev.target = save_dev;
  73.         gx_set_device_only(pgs, dev);
  74.        }
  75.     while ( dleft )
  76.        {    /* Fill up a row, then display it. */
  77.         uint bcount = min(dleft, rsize - pos);
  78.         byte *bptr =
  79.           penum->buffer + (pos << 3) / penum->bps * penum->spread;
  80.         int px;
  81.         for ( px = 0; px < penum->spread; px++ )
  82.           (*penum->unpack)(penum, &penum->map[px], bptr + px, penum->planes[px] + dpos, bcount, pos);
  83.         pos += bcount;
  84.         dpos += bcount;
  85.         dleft -= bcount;
  86.         if ( pos == rsize )    /* filled an entire row */
  87.            {
  88. #ifdef DEBUG
  89. if ( gs_debug['B'] )
  90.    {            int i, n = width * penum->spp;
  91.             dputs("[B]row:");
  92.             for ( i = 0; i < n; i++ )
  93.                 dprintf1(" %02x", penum->buffer[i]);
  94.             dputs("\n");
  95.    }
  96. #endif
  97.             if ( !penum->skewed )
  98.               { /* Precompute integer y and height, */
  99.                 /* and check for clipping. */
  100.                 fixed yc = penum->ycur, yn;
  101.                 fixed dyy = penum->fyy;
  102.                 fixed adjust = penum->adjust;
  103.                 if ( dyy > 0 )
  104.                   dyy += adjust << 1,
  105.                   yc -= adjust;
  106.                 else
  107.                   dyy = (adjust << 1) - dyy,
  108.                   yc -= dyy - adjust;
  109.                 if ( yc >= penum->clip_box.q.y ) goto mt;
  110.                 yn = yc + dyy;
  111.                 if ( yn <= penum->clip_box.p.y ) goto mt;
  112.                 penum->yci = fixed2int_var_rounded(yc);
  113.                 penum->hci =
  114.                   fixed2int_var_rounded(yn) - penum->yci;
  115.                 if ( penum->hci == 0 ) goto mt;
  116.               }
  117.             code = (*penum->render)(penum, penum->buffer, width * penum->spp, 1);
  118.             if ( code < 0 ) goto err;
  119. mt:            if ( ++(penum->y) == penum->height ) goto end;
  120.             pos = 0;
  121.             penum->xcur += penum->fyx;
  122.             penum->ycur += penum->fyy;
  123.            }
  124.        }
  125.     penum->byte_in_row = pos;
  126.     code = 0;
  127.     goto out;
  128. end:    /* End of data */
  129.     code = 1;
  130.     goto out;
  131. err:    /* Error or interrupt, restore original state. */
  132.     penum->plane_index = penum->spread - 1;
  133.     penum->xcur = xcur_save;
  134.     penum->ycur = ycur_save;
  135.     penum->y = y_save;
  136.     /* Note that caller must call gs_image_cleanup */
  137.     /* for both error and normal termination. */
  138. out:    if ( save_dev != 0 ) gx_set_device_only(pgs, save_dev);
  139.     return code;
  140. }
  141.  
  142. /* Clean up by releasing the buffers. */
  143. void
  144. gs_image_cleanup(register gs_image_enum *penum)
  145. {    if ( penum->buffer )
  146.     {    gs_free((char *)penum->buffer, 1, penum->buffer_size,
  147.             "image buffer");
  148.         penum->buffer = 0;
  149.     }
  150.     if ( penum->line )
  151.     {    gs_free((char *)penum->line, 1, penum->line_size,
  152.             "image line");
  153.         penum->line = 0;
  154.     }
  155. }
  156.  
  157. /* ------ Unpacking procedures ------ */
  158.  
  159. void
  160. image_unpack_1(const gs_image_enum *penum, const sample_map *pmap,
  161.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  162. {    register ulong *bufp = (unsigned long *)bptr;
  163.     int left = dsize;
  164.     register const ulong *map = &pmap->table.lookup4x1to32[0];
  165.     register uint b;
  166.     if ( left & 1 )
  167.        {    b = data[0];
  168.         bufp[0] = map[b >> 4];
  169.         bufp[1] = map[b & 0xf];
  170.         data++, bufp += 2;
  171.        }
  172.     left >>= 1;
  173.     while ( left-- )
  174.        {    b = data[0];
  175.         bufp[0] = map[b >> 4];
  176.         bufp[1] = map[b & 0xf];
  177.         b = data[1];
  178.         bufp[2] = map[b >> 4];
  179.         bufp[3] = map[b & 0xf];
  180.         data += 2, bufp += 4;
  181.        }
  182. }
  183.  
  184. void
  185. image_unpack_2(const gs_image_enum *penum, const sample_map *pmap,
  186.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  187. {    register ushort *bufp = (unsigned short *)bptr;
  188.     int left = dsize;
  189.     register const ushort *map = &pmap->table.lookup2x2to16[0];
  190.     while ( left-- )
  191.        {    register unsigned b = *data++;
  192.         *bufp++ = map[b >> 4];
  193.         *bufp++ = map[b & 0xf];
  194.        }
  195. }
  196.  
  197. void
  198. image_unpack_4(const gs_image_enum *penum, const sample_map *pmap,
  199.   register byte *bufp, register const byte *data, uint dsize, uint inpos)
  200. {    register int spread = penum->spread;
  201.     int left = dsize;
  202.     register const byte *map = &pmap->table.lookup8[0];
  203.     while ( left-- )
  204.        {    register unsigned b = *data++;
  205.         *bufp = map[b >> 4]; bufp += spread;
  206.         *bufp = map[b & 0xf]; bufp += spread;
  207.        }
  208. }
  209.  
  210. void
  211. image_unpack_8(const gs_image_enum *penum, const sample_map *ignore_pmap,
  212.   byte *bufp, const byte *data, uint dsize, uint inpos)
  213. {    if ( data != bufp ) memcpy(bufp, data, dsize);
  214. }
  215.  
  216. /* ------ Rendering procedures ------ */
  217.  
  218. /* Rendering procedure for ignoring an image.  We still need to iterate */
  219. /* over the samples, because the procedure might have side effects. */
  220. int
  221. image_render_skip(gs_image_enum *penum, byte *data, uint w, int h)
  222. {    return h;
  223. }
  224.  
  225. /* Rendering procedure for a monobit image with no */
  226. /* skew or rotation and pure colors. */
  227. int
  228. image_render_simple(gs_image_enum *penum, byte *buffer, uint w, int h)
  229. {    byte *line = penum->line;
  230.     uint line_size, line_width;
  231.     gx_device *dev = penum->pgs->device->info;
  232.     dev_proc_copy_mono((*copy_mono)) = dev->procs->copy_mono;
  233.     int ix = fixed2int_rounded(penum->xcur);
  234.     const int iy = penum->yci, ih = penum->hci;
  235.     gx_color_index
  236.         zero = penum->icolor0.color1,
  237.         one = penum->icolor1.color1;
  238.     int dy;
  239.  
  240.     if ( penum->map[0].table.lookup4x1to32[0] != 0 )
  241.         zero = penum->icolor1.color1,
  242.         one = penum->icolor0.color1;
  243.  
  244.     if ( line == 0 )
  245.     {    /* A direct BitBlt is possible. */
  246.         line = buffer;
  247.         line_size = (w + 7) >> 3;
  248.         line_width = w;
  249.     }
  250.     else
  251.     {    fixed xl = penum->xcur + fixed_half - int2fixed(ix);
  252.         const fixed dxx = penum->fxx;
  253.         const fixed dxx_4 = dxx << 2;
  254.         const fixed dxx_8 = dxx_4 << 1;
  255.         register const byte *psrc = buffer;
  256.         byte sbit = 0x80;
  257.         byte *endp = buffer + (w >> 3);
  258.         const byte endbit = 1 << (~w & 7);
  259.         byte data;
  260.  
  261.         line_size = penum->line_size;
  262.         line_width = penum->line_width;
  263.         if ( dxx < 0 )
  264.             ix -= line_width,
  265.             xl += int2fixed(line_width);
  266.  
  267.         /* Invert the bi